from selenium import webdriver
import time, xlrd
from selenium.webdriver.common.action_chains import ActionChains


# 登陆网站
def zhtj_login():
    drivers = webdriver.Chrome()
    drivers.get('https://zhtj.youth.cn/zhtj/')
    user_id = '410527198004140036'
    user_pw = '227718Lk'
    drivers.find_element_by_id('loginId').send_keys(user_id)
    drivers.find_element_by_id('password').send_keys(user_pw)
    user_yzm = input('Please input your yanking:')
    drivers.find_element_by_id('yzm').send_keys(user_yzm)
    drivers.find_element_by_id('subBut').click()
    return drivers


# 选择功能侧菜单
def side_menu(drivers, enter_menu=None):
    side_menu_dict = {}
    time.sleep(2)
    selected_son_menu = print_menu(drivers.find_element_by_id('side-menu').find_elements_by_tag_name('li'))
    time.sleep(1)
    # 有问题需要解决，元素display属性是False，所以无法显示菜单选择项,利用get_attribute('textContent')可以解决
    print_menu(selected_son_menu.find_elements_by_tag_name('li'), False)
    return drivers


# 打印菜单并选择
def print_menu(menu, switch=True):
    # print(menu)
    # 保存菜单索引号index和菜单内容的字典
    menu_dict = {}
    for index, value in enumerate(menu):
        if switch:
            if value.text != '':
                menu_dict[index] = value.text
        else:
            menu_dict[index] = value.get_attribute('textContent')
    # print(menu_dict)
    for key, value in menu_dict.items():
        print('       %d:%s' % (key, value))
    selected_menu_num = input('输入进入菜单数字:')
    selected_son_menu = menu[int(selected_menu_num)]
    selected_son_menu.click()
    return selected_son_menu


if __name__ == "__main__":
    driver = zhtj_login()
    side_menu(driver)
